package com.lingjtx.common.core;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;

import java.io.Serializable;

/**
 * 响应返回的json体
 *
 * @author John Liu
 */
public class ResponseResult<T> implements Serializable {
    private String requestId;
    private final int code;
    private final String message;
    private final T data;

    private ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private ResponseResult(int status) {
        this(status, null, null);
    }

    private ResponseResult(int status, String msg) {
        this(status, msg, null);
    }

    private ResponseResult(int status, T data) {
        this(status, null, data);
    }

    private ResponseResult(ResponseCode responseCode) {
        this(responseCode.getCode(), responseCode.getDesc());
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    // 使之不在json序列化结果当中
    @JSONField(serialize = false)
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static <T> ResponseResult<T> createBySuccessCode() {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ResponseResult<T> createBySuccess() {
        return new ResponseResult<>(ResponseCode.SUCCESS);
    }

    public static ResponseResult<String> createBySuccessToString(String data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc(), data);
    }

    public static <T> ResponseResult<T> createBySuccess(String msg) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseResult<T> createBySuccess(T data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc(), data);
    }

    public static <T> ResponseResult<T> createBySuccess(String msg, T data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseResult<T> createByError() {
        return new ResponseResult<>(ResponseCode.ERROR.getCode(),
                ResponseCode.ERROR.getDesc());
    }

    public static <T> ResponseResult<T> createByError(String errorMessage) {
        return new ResponseResult<>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ResponseResult<T> createByError(String errorMessage, T data) {
        return new ResponseResult<>(ResponseCode.ERROR.getCode(), errorMessage, data);
    }

    public static <T> ResponseResult<T> createByError(ResponseCode responseCode) {
        return new ResponseResult<>(responseCode.getCode(),
                responseCode.getDesc());
    }

    public static <T> ResponseResult<T> createByError(ResponseCode responseCode, T data) {
        return new ResponseResult<>(responseCode.getCode(),
                responseCode.getDesc(), data);
    }

    public static <T> ResponseResult<T> createByError(int errorCode,
                                                      String errorMessage) {
        return new ResponseResult<>(errorCode, errorMessage);
    }

    public static <T> ResponseResult<T> createByError(int errorCode,
                                                      String errorMessage,
                                                      T data) {
        return new ResponseResult<>(errorCode, errorMessage, data);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
